﻿
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace Assets.Editor.MEditor
{
    /// <summary>
    /// 将资源目录下面的文件写入配置文件
    /// </summary>
    public class WriteResConfig : ScriptableWizard
    {
        //1.将Resources下面的文件中的Audio下面的声音写入到配置文件中
        //2.找到Audio文件

        private static Dictionary<string,string>namePathDict=new Dictionary<string, string>();

        [MenuItem("Assets/WriteResConfig")]
        static void CreatEnCriptionWindow()
        {
            ScriptableWizard.DisplayWizard<WriteResConfig>("写入资源配置", "WriteResConfig");
        }

        void OnWizardCreate()
        {
            Debug.Log("写入资源的配置文件");
            string resPath = @"E:/UnityProjects/IAClient/Assets/Resources/Audio";
            string outPath = @"E:/UnityProjects/IAClient/Assets/Resources/ResConfig";
            Debug.Log("resPath:"+resPath);
            Debug.Log("outPath:"+outPath);
            WirteResConfigFile(resPath,outPath);
        }

        /// <summary>
        /// 写入资源的配置文件
        /// </summary>
        /// <param name="resPath"></param>
        /// <param name="outPath"></param>
        /// <param name="pathNode"></param> 
        static void WirteResConfigFile(string resPath, string outPath)
        {
            if (string.IsNullOrEmpty(resPath))
            {
                Debug.LogError("资源路径为空");
                return;
            }
            DirectoryInfo dirInfo = new DirectoryInfo(resPath);
            if (!dirInfo.Exists)
            {
                Debug.LogError("不存在路径：" + resPath);
                return;
            }
            else//资源存在
            {
                FileSystemInfo[] fileSystemInfos = dirInfo.GetFileSystemInfos();
                for (int i = 0; i < fileSystemInfos.Length; i++)
                {
                    OpResByDirects(fileSystemInfos[i]);
                }
                string fileName = resPath.Substring(resPath.LastIndexOf("/"))+"Config.bytes";
                //Debug.Log("fileName:" + fileName);
                //Debug.Log(namePathDict.Count);
                //写入配置文件并且保存
                WriteResConfigFile(namePathDict,outPath,fileName);
            }
            AssetDatabase.Refresh();
        }

        static void WriteResConfigFile(Dictionary<string, string> namePathDict,string outPath,string fileName)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(outPath);
            Debug.Log(namePathDict.Count);
            if (!directoryInfo.Exists)
            {
                directoryInfo.Create();
            }
            string filePath =outPath + "/" + fileName;
            using (StreamWriter sw =File.CreateText(filePath))
            {
                try
                {
                    foreach (KeyValuePair<string, string> pair in namePathDict)
                    {
                        sw.WriteLine(pair.Key + "," + pair.Value);
                    }
                }
                catch (Exception e)
                {
                    Debug.Log(e.ToString());
                }
            }     
        }

        /// <summary>
        /// 读取资源
        /// </summary>
        static void OpResByDirects(FileSystemInfo fileSystemInfo)
        {
            //Debug.Log(fileSystemInfo.Extension);
            if (!fileSystemInfo.Exists || fileSystemInfo.Extension ==".meta")
            {
                return;
            }
            DirectoryInfo directoryInfo = new DirectoryInfo(fileSystemInfo.FullName);
            //Debug.Log("fileSystemInfo.FullName:"+fileSystemInfo.FullName);
            FileSystemInfo[] fileSystemInfos = directoryInfo.GetFileSystemInfos();
            foreach (var systemInfo in fileSystemInfos)
            {
                if (!systemInfo.Exists || systemInfo.Extension == ".meta")
                {
                    continue;
                }
                FileInfo fileInfo = systemInfo as FileInfo;
                if (fileInfo == null)
                {
                    //代表强转失败，是文件夹
                    //如果访问的是文件夹：再继续访问里面的文件系统，直到找到我恩健
                    OpResByDirects(systemInfo);
                }
                else//找到文件
                {
                    int index = systemInfo.FullName.IndexOf("Resources");
                    string resPath = systemInfo.FullName.Substring(index);
                    string resPath0 = resPath.Remove(0, 10);//去掉Resources/
                    if(namePathDict.ContainsKey(systemInfo.Name))return;
                    namePathDict.Add(systemInfo.Name,resPath0);
                }
            }
        }
    }
}
